package cn.smbms.controller;

import cn.smbms.pojo.Provider;
import cn.smbms.pojo.StorageRecord;
import cn.smbms.pojo.User;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.service.storagerecord.StorageRecordService;
import cn.smbms.tools.Constants;
import cn.smbms.tools.PageSupport;
import com.mysql.cj.util.StringUtils;

import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Auther: liyuxuan
 * @Date:2021/11/8 - 11 - 08 - 10:21
 */
@Log4j
@Controller
@RequestMapping("/storageRecord")
public class StorageRecordController {
    @Autowired
    private StorageRecordService storageRecordService;
    @Autowired
    private ProviderService providerService;
    @RequestMapping("/sList")
    public String storageRecordList(Model model,String queryProductName, String queryProviderId, String queryIsPayment,@RequestParam(defaultValue = "1") Integer pageIndex){
        StorageRecord bill = new StorageRecord();
        if(StringUtils.isNullOrEmpty(queryProductName)){
            queryProductName = "";
        }
        model.addAttribute("providerList", providerService.getProviderList("","",1,50));
        List<StorageRecord> billList = null;

        System.out.println("queryProductName servlet--------"+queryProductName);
        System.out.println("queryProviderId servlet--------"+queryProviderId);
        System.out.println("queryIsPayment servlet--------"+queryIsPayment);
        System.out.println("query pageIndex--------- > " + pageIndex);
        if(StringUtils.isNullOrEmpty(queryIsPayment)){
            bill.setIsPayment(0);
        }else{
            bill.setIsPayment(Integer.parseInt(queryIsPayment));
        }

        if(StringUtils.isNullOrEmpty(queryProviderId)){
            bill.setProviderId(0);
        }else{
            bill.setProviderId(Integer.parseInt(queryProviderId));
        }
        bill.setProductName(queryProductName);
        //设置页面总量
        int pageSize = Constants.pageSize;
        //总数量
        int totalCount = storageRecordService.count(bill);
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(pageIndex);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        if (pageIndex > totalPageCount) {
            pageIndex = totalPageCount;
            throw new RuntimeException("页码不正确");
        }

        billList = storageRecordService.getStorageRecordList(bill,pageIndex,pageSize);
        model.addAttribute("billList", billList);
        model.addAttribute("queryProductName", queryProductName);
        model.addAttribute("queryProviderId", queryProviderId);
        model.addAttribute("queryIsPayment", queryIsPayment);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",pageIndex);
        return "/jsp/storagelist";
    }
    @GetMapping("/view/{id}")
    public String view(@PathVariable String id,Model model){
        log.debug("根据ID查询入库记录详情"+id);
        StorageRecord storageRecord = storageRecordService.getBillById(id);
        model.addAttribute(storageRecord);
        return "/jsp/storageview";
    }

    @GetMapping("/toModify/{id}")
    public String toModify(@PathVariable String id,Model model){
        StorageRecord storageRecord = storageRecordService.getBillById(id);
        model.addAttribute(storageRecord);
        return "/jsp/storagemodify";
    }

    @PostMapping("/modify")
    public String modify(StorageRecord storageRecord, HttpSession session){
        storageRecord.setModifyBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        storageRecord.setModifyDate(new Date());
        if (storageRecordService.modify(storageRecord)) {
            return "redirect:/storageRecord/sList";
        }
        return "/jsp/storagemodify";
    }
    @ResponseBody
    @GetMapping("/getProvider")
    public Object getProvider(){
        List<Provider> providerList = providerService.getProviderList("", "", 1, 50);
        return providerList;
    }
    @PostMapping("/addStorageRecord")
    public String addStorageRecord(StorageRecord storageRecord,HttpSession session){
        storageRecord.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        storageRecord.setCreationDate(new Date());
        if(storageRecordService.add(storageRecord)){
            return "redirect:/storageRecord/sList";
        }
        return "/jsp/storageadd";
    }
    @GetMapping("/storageRecordDel/{id}")
    public Object storageRecordDel(@PathVariable String id,Model model){
        if(storageRecordService.getBillById(id)!=null){
            if(storageRecordService.deleteBillById(id)){
                return model.addAttribute("delResult","true");
            } else {
                return model.addAttribute("delResult","false");
            }
        } else {
            return model.addAttribute("delResult","notexist");
        }
    }
}
